﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;
using System.Xml;
using Newtonsoft.Json;
using WeiSha.Core.Param.Method;

namespace WeiSha.Core.Templates
{
	// Token: 0x02000033 RID: 51
	[JsonObject(0)]
	public class TemplateBank
	{
		// Token: 0x170000A9 RID: 169
		// (get) Token: 0x060001D3 RID: 467 RVA: 0x0000D944 File Offset: 0x0000BB44
		// (set) Token: 0x060001D4 RID: 468 RVA: 0x0000D95C File Offset: 0x0000BB5C
		public string Name
		{
			get
			{
				return this._name;
			}
			set
			{
				this._name = value;
			}
		}

		// Token: 0x170000AA RID: 170
		// (get) Token: 0x060001D5 RID: 469 RVA: 0x0000D968 File Offset: 0x0000BB68
		// (set) Token: 0x060001D6 RID: 470 RVA: 0x0000D980 File Offset: 0x0000BB80
		public string Tag
		{
			get
			{
				return this._tag;
			}
			set
			{
				this._tag = value;
			}
		}

		// Token: 0x170000AB RID: 171
		// (get) Token: 0x060001D7 RID: 471 RVA: 0x0000D98C File Offset: 0x0000BB8C
		// (set) Token: 0x060001D8 RID: 472 RVA: 0x0000D9A4 File Offset: 0x0000BBA4
		public string Author
		{
			get
			{
				return this._author;
			}
			set
			{
				this._author = value;
			}
		}

		// Token: 0x170000AC RID: 172
		// (get) Token: 0x060001D9 RID: 473 RVA: 0x0000D9B0 File Offset: 0x0000BBB0
		// (set) Token: 0x060001DA RID: 474 RVA: 0x0000D9C8 File Offset: 0x0000BBC8
		public DateTime CrtTime
		{
			get
			{
				return this._crtTime;
			}
			set
			{
				this._crtTime = value;
			}
		}

		// Token: 0x170000AD RID: 173
		// (get) Token: 0x060001DB RID: 475 RVA: 0x0000D9D4 File Offset: 0x0000BBD4
		// (set) Token: 0x060001DC RID: 476 RVA: 0x0000D9EC File Offset: 0x0000BBEC
		public string QQ
		{
			get
			{
				return this._qq;
			}
			set
			{
				this._qq = value;
			}
		}

		// Token: 0x170000AE RID: 174
		// (get) Token: 0x060001DD RID: 477 RVA: 0x0000D9F8 File Offset: 0x0000BBF8
		// (set) Token: 0x060001DE RID: 478 RVA: 0x0000DA10 File Offset: 0x0000BC10
		public string Phone
		{
			get
			{
				return this._phone;
			}
			set
			{
				this._phone = value;
			}
		}

		// Token: 0x170000AF RID: 175
		// (get) Token: 0x060001DF RID: 479 RVA: 0x0000DA1C File Offset: 0x0000BC1C
		// (set) Token: 0x060001E0 RID: 480 RVA: 0x0000DA34 File Offset: 0x0000BC34.
		public string Intro
		{
			get
			{
				return this._intro;
			}
			set
			{
				this._intro = value;
			}
		}

        // Token: 0x170000B0 RID: 176
        // (get) Token: 0x060001E1 RID: 481 RVA: 0x0000DA40 File Offset: 0x0000BC40

        public string Size
		{
			get
			{
				return this._size;
			}
		}

		// Token: 0x170000B1 RID: 177
		// (get) Token: 0x060001E2 RID: 482 RVA: 0x0000DA58 File Offset: 0x0000BC58
		public ulong Length
		{
			get
			{
				return this._length;
			}
		}

		// Token: 0x170000B2 RID: 178
		// (get) Token: 0x060001E3 RID: 483 RVA: 0x0000DA70 File Offset: 0x0000BC70
		public string Logo
		{
			get
			{
				return this.Path.Virtual + "logo.jpg";
			}
		}

		// Token: 0x170000B3 RID: 179
		// (get) Token: 0x060001E4 RID: 484 RVA: 0x0000DA98 File Offset: 0x0000BC98
		// (set) Token: 0x060001E5 RID: 485 RVA: 0x0000DAB0 File Offset: 0x0000BCB0
		[JsonIgnore]
		public _Path Path
		{
			get
			{
				return this._path;
			}
			set
			{
				this._path = value;
			}
		}

		// Token: 0x170000B4 RID: 180
		// (get) Token: 0x060001E6 RID: 486 RVA: 0x0000DABC File Offset: 0x0000BCBC
		public string Virtual
		{
			get
			{
				return this._path.Virtual;
			}
		}

		// Token: 0x170000B5 RID: 181
		// (get) Token: 0x060001E7 RID: 487 RVA: 0x0000DADC File Offset: 0x0000BCDC
		// (set) Token: 0x060001E8 RID: 488 RVA: 0x0000DAF4 File Offset: 0x0000BCF4
		public bool IsCurrent
		{
			get
			{
				return this._isCurrent;
			}
			set
			{
				this._isCurrent = value;
			}
		}

		// Token: 0x170000B6 RID: 182
		// (get) Token: 0x060001E9 RID: 489 RVA: 0x0000DB00 File Offset: 0x0000BD00
        [JsonIgnore]
		public TemplateConfingItem Config
		{
			get
			{
				return this._config;
			}
		}

		// Token: 0x170000B7 RID: 183
		// (get) Token: 0x060001EA RID: 490 RVA: 0x0000DB18 File Offset: 0x0000BD18
		// (set) Token: 0x060001EB RID: 491 RVA: 0x0000DB20 File Offset: 0x0000BD20
		[JsonIgnore]
		public DirectoryInfo Directory { get; set; }

		// Token: 0x170000B8 RID: 184
		// (get) Token: 0x060001EC RID: 492 RVA: 0x0000DB29 File Offset: 0x0000BD29
		// (set) Token: 0x060001ED RID: 493 RVA: 0x0000DB31 File Offset: 0x0000BD31
		[JsonIgnore]
		public PlateOrganInfo OrganInfo { get; set; }

        // Token: 0x060001EE RID: 494 RVA: 0x0000DB3C File Offset: 0x0000BD3C
        public TemplateBank(DirectoryInfo di, TemplateConfingItem config)
		{
			this._config = config;
			this.Directory = di;
			this._init(di);
		}

		// Token: 0x060001EF RID: 495 RVA: 0x0000DB8C File Offset: 0x0000BD8C
		private void _init(DirectoryInfo di)
		{
			string text = di.FullName + "\\" + _xmlName;
			bool flag = !File.Exists(text);
			if (flag)
			{
				this._creatXML(text);
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Template");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode("Info");
			foreach (object obj in xmlNode2.ChildNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				XmlElement xmlElement = (XmlElement)xmlNode3;
				bool flag2 = xmlElement.Name == "Name";
				if (flag2)
				{
					this._name = (string.IsNullOrWhiteSpace(xmlElement.InnerText) ? di.Name : xmlElement.InnerText);
				}
				bool flag3 = xmlElement.Name == "Author";
				if (flag3)
				{
					this._author = xmlElement.InnerText;
				}
				bool flag4 = xmlElement.Name == "CrtTime";
				if (flag4)
				{
					try
					{
						this._crtTime = (string.IsNullOrWhiteSpace(xmlElement.InnerText) ? DateTime.Now : Convert.ToDateTime(xmlElement.InnerText));
					}
					catch
					{
						this._crtTime = DateTime.Now;
					}
				}
				bool flag5 = xmlElement.Name == "QQ";
				if (flag5)
				{
					this._qq = xmlElement.InnerText;
				}
				bool flag6 = xmlElement.Name == "Phone";
				if (flag6)
				{
					this._phone = xmlElement.InnerText;
				}
				bool flag7 = xmlElement.Name == "Intro";
				if (flag7)
				{
					this._intro = xmlElement.InnerText;
				}
			}
			this._length = Request.Path(di.FullName).Length;
			this._size = Request.Path("").GetSize(this._length);
			this._getPages(di, null);
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
			fileSystemWatcher.Path = di.FullName;
			fileSystemWatcher.Filter = "*.*";
			fileSystemWatcher.Changed += this._dirChangeed;
			fileSystemWatcher.Created += this._dirChangeed;
			fileSystemWatcher.Deleted += this._dirChangeed;
			fileSystemWatcher.Renamed += new RenamedEventHandler(this._dirChangeed);
			fileSystemWatcher.EnableRaisingEvents = true;
			fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.CreationTime | NotifyFilters.Security;
			fileSystemWatcher.IncludeSubdirectories = true;
		}

		// Token: 0x060001F0 RID: 496 RVA: 0x0000DE5C File Offset: 0x0000C05C
		private void _dirChangeed(object source, FileSystemEventArgs e)
		{
			Cache cache = HttpRuntime.Cache;
			bool flag = cache == null;
			if (!flag)
			{
				string text = string.Format(TemplateBank._cacheKey, this.Tag, this.Name);
				cache.Remove(text);
			}
		}

		// Token: 0x060001F1 RID: 497 RVA: 0x0000DE9C File Offset: 0x0000C09C
		private void _getPages(DirectoryInfo root, DirectoryInfo di)
		{
			bool flag = di == null;
			if (flag)
			{
				di = root;
			}
			foreach (FileInfo fileInfo in di.GetFiles("*.html"))
			{
				bool flag2 = fileInfo.Name.IndexOf(".") < 0;
				if (!flag2)
				{
					string text = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf("."));
					string text2 = di.FullName.Substring(root.FullName.Length);
					bool flag3 = text2.StartsWith("\\");
					if (flag3)
					{
						text2 = text2.Substring(1);
					}
					text = (string.IsNullOrWhiteSpace(text2) ? "" : (text2 + "\\")) + text;
					bool flag4 = !this._pages.Contains(text);
					if (flag4)
					{
						this._pages.Add(text);
					}
					bool flag5 = !this._pages.Contains(fileInfo.FullName);
					if (flag5)
					{
						this._pagesPath.Add(fileInfo.FullName);
					}
				}
			}
			foreach (DirectoryInfo directoryInfo in di.GetDirectories())
			{
				this._getPages(root, directoryInfo);
			}
		}

		// Token: 0x060001F2 RID: 498 RVA: 0x0000DFF0 File Offset: 0x0000C1F0
		private void _creatXML(string xmlPath)
		{
			StreamWriter streamWriter = new StreamWriter(xmlPath, true, Encoding.UTF8);
			streamWriter.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
			streamWriter.WriteLine("<Template>");
			streamWriter.WriteLine("<Info>");
			streamWriter.WriteLine("<Name/>");
			streamWriter.WriteLine("<Author/>");
			streamWriter.WriteLine("<CrtTime/>");
			streamWriter.WriteLine("<QQ/>");
			streamWriter.WriteLine("<Phone/>");
			streamWriter.WriteLine("<Intro/>");
			streamWriter.WriteLine("</Info>");
			streamWriter.WriteLine("</Template>");
			streamWriter.Close();
			streamWriter.Dispose();
		}

		// Token: 0x060001F3 RID: 499 RVA: 0x0000E0A0 File Offset: 0x0000C2A0
		public void Save()
		{
			string text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
			text += "<Template>";
			text += "<Info>";
			text = text + "<Name>" + this._name + "</Name>";
			text = text + "<Author>" + this._author + "</Author>";
			text = text + "<CrtTime>" + this.CrtTime.ToString() + "</CrtTime>";
			text = text + "<QQ>" + this.QQ + "</QQ>";
			text = text + "<Phone>" + this.Phone + "</Phone>";
			text = text + "<Intro>" + this.Intro + "</Intro>";
			text += "</Info>";
			text += "</Template>";
			string text2 = this.Path.Physics + "\\" + TemplateBank._xmlName;
			using (StreamWriter streamWriter = new StreamWriter(text2, false, Encoding.UTF8))
			{
				streamWriter.Write(text);
			}
		}

		// Token: 0x170000B9 RID: 185
		// (get) Token: 0x060001F4 RID: 500 RVA: 0x0000E1C8 File Offset: 0x0000C3C8
		[JsonIgnore]
		public List<string> Pages
		{
			get
			{
				object pageslocker = TemplateBank._pageslocker;
				List<string> list;
				lock (pageslocker)
				{
					this._getPages(this.Directory, null);
					Cache cache = HttpRuntime.Cache;
					bool flag2 = cache == null;
					if (flag2)
					{
						list = this._pages;
					}
					else
					{
						string text = string.Format(TemplateBank._cacheKey, this.Tag, this.Name);
						cache.Insert(text, this._pages, new CacheDependency(this._pagesPath.ToArray()));
						list = this._pages;
					}
				}
				return list;
			}
		}

		// Token: 0x060001F5 RID: 501 RVA: 0x0000E26C File Offset: 0x0000C46C
		public bool PageExists(string pageName)
		{
			bool flag2;
			try
			{
				bool flag = string.IsNullOrWhiteSpace(pageName);
				if (flag)
				{
					flag2 = false;
				}
				else
				{
					bool flag3 = pageName.IndexOf(".") > -1;
					if (flag3)
					{
						pageName = pageName.Substring(0, pageName.LastIndexOf("."));
					}
					List<string> list = null;
					Cache cache = HttpRuntime.Cache;
					string cachekey = string.Format(TemplateBank._cacheKey, this.Tag, this.Name);
					object obj = cache.Get(cachekey);
					bool flag4 = obj != null;
					if (flag4)
					{
						list = (List<string>)obj;
					}
					bool flag5 = false;
					bool flag6 = list != null;
					if (flag6)
					{
						flag5 = list.Exists((string e) => pageName.Equals(e, StringComparison.CurrentCultureIgnoreCase));
						bool flag7 = flag5;
						if (flag7)
						{
							return true;
						}
					}
					bool flag8 = !flag5;
					if (flag8)
					{
						this._getPages(this.Directory, null);
						new Task(delegate
						{
							lock (this)
							{
								cache.Insert(cachekey, this._pages, new CacheDependency(this._pagesPath.ToArray()));
							}
						}).Start();
					}
					flag2 = this._pages.Exists((string e) => pageName.Equals(e, StringComparison.CurrentCultureIgnoreCase));
				}
			}
			catch (Exception ex)
			{
				Log.Error(base.GetType().Name, ex);
				throw ex;
			}
			return flag2;
		}

		// Token: 0x04000087 RID: 135
		private static string _xmlName = "_self.xml";

		// Token: 0x04000088 RID: 136
		private static string _cacheKey = "TEMPLATEBANK_CACHE_PAGE_{0}_{1}";

		// Token: 0x04000089 RID: 137
		private string _name;

		// Token: 0x0400008A RID: 138
		private string _tag;

		// Token: 0x0400008B RID: 139
		private string _author;

		// Token: 0x0400008C RID: 140
		private DateTime _crtTime;

		// Token: 0x0400008D RID: 141
		private string _qq;

		// Token: 0x0400008E RID: 142
		private string _phone;

		// Token: 0x0400008F RID: 143
		private string _intro;

		// Token: 0x04000090 RID: 144
		private string _size;

		// Token: 0x04000091 RID: 145
		private ulong _length;

		// Token: 0x04000092 RID: 146
		private _Path _path;

		// Token: 0x04000093 RID: 147
		private bool _isCurrent = false;

		// Token: 0x04000094 RID: 148
		private TemplateConfingItem _config = null;

		// Token: 0x04000097 RID: 151
		private List<string> _pages = new List<string>();

		// Token: 0x04000098 RID: 152
		private List<string> _pagesPath = new List<string>();

		// Token: 0x04000099 RID: 153
		private static readonly object _pageslocker = new object();
	}
}
